package Beans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import Entity.*;
import Helper.Helper_Outlet;
import Helper.Helper_Stakeholder_Food;
import java.util.ArrayList;
import javax.persistence.Query;

/**
 *
 * @author TWD
 */
@Stateless
public class StakeholderAccountsBean implements StakeholderAccountsBeanRemote {
    
    @PersistenceContext()
    EntityManager em;
    
    private Stakeholder stakeholder;
    private Stakeholder_Food stakeholderFood;
    private StakeholderTier tier;
    
    public StakeholderAccountsBean(){}
    
    public boolean createStakeholder(String name, Integer loyaltyPoints, String email, String stakeholderTier){
     
            if (checkStakeholderName(name)) {
            stakeholderTier = stakeholderTier.toUpperCase();
            tier = em.find(StakeholderTier.class, stakeholderTier);
            stakeholder = new Stakeholder();
            stakeholder.create(name, loyaltyPoints, tier, email);
            em.persist(stakeholder);       
            return true;
            } else
                return false;
    
    }
    
    public boolean createStakeholderFood(String name, Integer loyaltyPoints, String email, String stakeholderTier){
                
            if (checkStakeholderName(name)) {
            //Checking the validity of the name and the tier
            stakeholderTier = stakeholderTier.toUpperCase();
            tier = em.find(StakeholderTier.class, stakeholderTier);
            
            //Setting the Stakeholder entity
            stakeholderFood = new Stakeholder_Food();
            stakeholderFood.setName(name);
            stakeholderFood.setEmail(email);
            stakeholderFood.setLoyaltyPoints(loyaltyPoints);
            stakeholderFood.setStakeholderTier(tier);
            em.persist(stakeholderFood);
            return true;
             } else
                return false;

    }
    
    public boolean checkStakeholderName(String name){
        
        stakeholder = em.find(Stakeholder.class, name);
        if(stakeholder == null){
            return true;
        }else{
            return false;
        }
    }

    
    
    public boolean deleteStakeholder(String name){      
        boolean bool = true;
        stakeholder = em.find(Stakeholder.class, name);
        if(stakeholder == null){
            return false;
        }else{
            Query q1 = em.createQuery("SELECT c FROM StakeholderBill c WHERE c.stakeHolderName = :paramName");
            q1.setParameter("paramName", name);
            
            List resultList = q1.getResultList();
            
            if (!resultList.isEmpty()){
                for (Object o : q1.getResultList()) {
                    StakeholderBill bill = (StakeholderBill) o;
                    if(bill.isHasPaid()){
                        bool = false;
                    }
                }
            }
            if(bool == true){
                stakeholder.setStakeholderTier(null);
                em.remove(stakeholder);
                System.out.println("StakeholderAccountsBean: Stakeholder deleted: " + stakeholder.getName());
                return true;
            }else{
                return false;
            }
        }
    }
    
    public boolean changeStakeholderTier(String stringTier, String name){
        try{
            
            tier = em.find(StakeholderTier.class, stringTier);
            stakeholder = em.find(Stakeholder.class, name);
            stakeholder.setStakeholderTier(tier);
            em.flush();
            return true;
        
        }catch(Exception ex){
            return false;
        }       
    }

    
    
    public List<Stakeholder> getAllStakeholders() {
        Query query = em.createQuery("SELECT e FROM Stakeholder e");
        return query.getResultList();
    }
    
    public List<Helper_Stakeholder_Food> getAllStakeholderFoodHelper(){
        Query query = em.createQuery("SELECT s FROM Stakeholder_Food s");
        List<Stakeholder_Food> stakeholders = query.getResultList();
        List<Helper_Stakeholder_Food> returnList = new ArrayList<Helper_Stakeholder_Food>();
        for (Stakeholder_Food s: stakeholders){
            Helper_Stakeholder_Food aStakeholder = new Helper_Stakeholder_Food (s.getName());
            returnList.add(aStakeholder);
            List<Outlet> outs = (List<Outlet>)s.getOutlet();
            if (outs == null)
                continue;
            for (Outlet o: outs){
                aStakeholder.addOutlet(new Helper_Outlet(o.getId(), o.getOutletName(),
                        o.getAddress(), s.getName(), o.getPhoneNumber()));
            }
        }
        return returnList;
    }
    
    public List<Stakeholder> getSpecificStakeholders(String name) {
        Query query = em.createQuery("SELECT OBJECT(e) FROM Stakeholder e where LOCATE(:name, e.name)>0");
        query.setParameter("name", name);
        return query.getResultList();
    }
    
    public List<Helper_Outlet> getOutletsByStakeholder(String sname){
        System.out.println("sname: "+ sname);
        Stakeholder_Food s = em.find(Stakeholder_Food.class, sname);
        List<Outlet> list = s.getOutlet();
        List<Helper_Outlet> returnList = new ArrayList<Helper_Outlet>();
        for (Outlet o: list){
            returnList.add(new Helper_Outlet(o.getId(), o.getOutletName(), o.getAddress(),
                    s.getName(), o.getPhoneNumber()));
        }
        System.out.println(returnList);
        return returnList;
    }
    
    
    //Randall added: 19-oct-2011
    public void givePointsToStakeholder(String stakeholderName, int pointsToGive){
        System.out.println(">>> StakeholderAccountsBean: Awarding points to stakeholder...");
        stakeholder = em.find(Stakeholder.class, stakeholderName);
        if(stakeholder == null){
            System.out.println(">>> StakeholderAccountsBean: Unable to find stakeholder");
            return;
        }
        int points = stakeholder.getLoyaltyPoints() + pointsToGive;
        stakeholder.setLoyaltyPoints(points);
        em.flush();
    }
    
    
    
    
    
    
    
    
}

    